package editadmin2;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
#
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
#
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use Data::Validate::Domain qw(is_domain);
use base 'func', 'editadmin', 'createadmin2';

BEGIN {
    require 'func.pm';
}

my $func = func->new();

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub valid_param_editadmin {
    my $self = shift;
    $self->SUPER::valid_param_createadmin(@_);
    return $self;
}

sub valid_param_editadmin2 {
    my $self = shift;

    if ( $self->{p}->{fPassword} ne '' ) {

        if (
               ( !length( $self->{p}->{fPassword} ) )
            || ( 100 < length( $self->{p}->{fPassword} ) )
            || ( $self->{se}->{max_long_passwd} >
                length( $self->{p}->{fPassword} ) )
          )
        {
            $self->{he}->{fPass_correct} = 1;
        }

        if ( $self->{p}->{fPassword} ne $self->{p}->{fPassword2} ) {
            $self->{he}->{fPass2_correct} = 1;
        }
    }

    my $username_sql;
    if ( $self->{he}->{fUsername_correct} != 1 ) {

        my $sth = $self->{dbh}->prepare(
            'SELECT id,
                                  name,
                                  pass,
                                  info
                         FROM users 
                         WHERE name = ? 
                         LIMIT 1  
                             '
        );
        $sth->execute( $self->{p}->{fUsername} );
        $username_sql = $sth->fetchrow_hashref();
        $sth->finish();

        if ( $username_sql->{name} ne $self->{p}->{fUsername} ) {
            $self->{he}->{fUsername_correct_exist} = 1;
        }

        $self->{p}->{id} = undef;
        $self->{p}->{id} = $username_sql;

    }

    if ( $self->{p}->{fPassword} ne '' ) {

        $self->{p}->{fPassword} = $func->md5crypt( $self->{p}->{fPassword} );
    }
    else {
        $self->{p}->{fPassword} = $username_sql->{pass};
    }

    $self->{he}->{no_fDomains_error} = undef;

    return $self;
}

sub edit_admin {
    my $self = shift;

    my $fount;
    foreach my $value ( values( %{ $self->{he} } ) ) {
        if ( $value == 1 ) { $fount = 1; last; }
    }
    if ( !$fount ) {

        my $sth = $self->{dbh}->prepare( "
                      UPDATE users 
                           SET 
                           pass = ?,
                           info = ?
                           
                           WHERE id = ?
               " );
        $sth->execute(
            $self->{p}->{fPassword},
            $self->{p}->{fCreateddomain},
            $self->{p}->{id}->{id}
        );
        $sth->finish();

        if ( $self->{p}->{fCreateddomain} == 1 ) {

            if ( $self->{p}->{id}->{info} == 1 ) {
                my $sth = $self->{dbh}->prepare(
                    '              
              UPDATE admin_domain 
                            SET m_alias = ?,
                            m_mailbox = ?,
                            m_domain = ?,
                            mb_domain = ?, 
                            mb_mailbox = ? 
                            WHERE idname = ?                            
                               '
                );
                $sth->execute(
                    $self->{p}->{fMaxalias},
                    $self->{p}->{fMaxmailbox},
                    $self->{p}->{fMaxdomain},
                    $self->{p}->{fMaxsizedomain},
                    $self->{p}->{fMaxsizemailbox},
                    $self->{p}->{id}->{id}
                );
                $sth->finish();

            }
            else {

                my $sth = $self->{dbh}->prepare( '
                          INSERT INTO admin_domain (idname,
                                        m_alias, 
                                        m_mailbox, 
                                        m_domain, 
                                        mb_domain, 
                                        mb_mailbox) 
                          VALUES (?,?,?,?,?,?)
                      ' );
                $sth->execute(
                    $self->{p}->{id}->{id},
                    $self->{p}->{fMaxalias},
                    $self->{p}->{fMaxmailbox},
                    $self->{p}->{fMaxdomain},
                    $self->{p}->{fMaxsizedomain},
                    $self->{p}->{fMaxsizemailbox}
                );
                $sth->finish();
            }
        }
        else {

            if ( $self->{p}->{id}->{info} == 1 ) {
                $self->{dbh}
                  ->do( qq{DELETE from admin_domain WHERE idname = ? LIMIT 1},
                    undef, $self->{p}->{id}->{id} );

                $self->{dbh}
                  ->do( qq{UPDATE users SET info = ? WHERE id = ? LIMIT 1},
                    undef, '0', $self->{p}->{id}->{id} );
            }
        }

        my @fDomains = ();

        @fDomains = split( "\0", $self->{p}->{fDomains} );

        if (@fDomains) {

            my $sth = $self->{dbh}->prepare(
                qq{SELECT domain 
                         FROM domain
                         WHERE idnane = ?}
            );
            $sth->execute( $self->{p}->{id}->{id} );

            my @usedomain;
            while ( $_ = $sth->fetchrow_hashref() ) {
                push( @usedomain, $_->{domain} );
            }

            $sth->finish();

            my %seen;
            @seen{@usedomain} = ();
            delete @seen{@fDomains};
            my @to_admin = keys %seen;

            my %seen;
            @seen{@fDomains} = ();
            delete @seen{@usedomain};
            my @to_user = keys %seen;

            if (@to_admin) {

                foreach $_ (@to_admin) {
                    $self->{dbh}->do(
                        qq{UPDATE domain
                                  SET idnane = ?
                                  WHERE domain = ?},
                        undef, '5', $_
                    );
                }

            }

            if (@to_user) {

                foreach $_ (@to_user) {
                    $self->{dbh}->do(
                        qq{UPDATE domain
                                  SET idnane = ?
                                  WHERE domain = ?},
                        undef, $self->{p}->{id}->{id}, $_
                    );
                }

            }

        }

        $self->{he}->{admin_added} = 1;
    }
    return $self;
}

sub return_edit_admin2 {
    shift->SUPER::return_edit_admin(@_);
}

sub templ_hash_admin {
    my $self = shift;

    $self->{session}->{filet} = 'edit-admin';
    $self->SUPER::templ_hash(@_);

}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
